package controller;

import formbeans.DeleteUserForm;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databeans.User;
import model.Model;
import model.UserDAO;
/*import model.AnswerDAO;
import model.SurveyDAO;
import model.Qid_EntityDAO;
import model.Qid_QuestionDAO;
import model.Sid_RespondentDAO;
import model.QuestionDAO;
*/
public class DeleteUserAction extends Action {
	private FormBeanFactory<DeleteUserForm> formBeanFactory = FormBeanFactory.getInstance(DeleteUserForm.class);
	private UserDAO userDAO;
	/*private AnswerDAO answerDAO;
	private SurveyDAO surveyDAO;
	private Qid_EntityDAO qidEntityDAO;
	private Qid_QuestionDAO qidQuestionDAO;
	private Sid_RespondentDAO sidRespondentDAO;
	private QuestionDAO questionDAO;*/
	//instantiation.
	public DeleteUserAction(Model model) {
		userDAO = model.getUserDAO();
		/*answerDAO = model.getAnswerDAO();
		surveyDAO = model.getSurveyDAO();
		qidEntityDAO = model.getQid_EntityDAO();
		qidQuestionDAO = model.getQid_QuestionDAO();
		sidRespondentDAO = model.getSid_RespondentDAO();
		questionDAO = model.getQuestionDAO();	*/
	}
    //return Name.
	public String getName() {
		return "deleteuser.do";
	}
    
    public String perform(HttpServletRequest request) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        String button = request.getParameter("button");
        HttpSession session = request.getSession(false);

		if (button == null)
			return "admin.jsp";
		if (button.equals("back")){
			return "admin.jsp";
		}
		String userid = request.getParameter("value");
		User showuser;
		try {
			showuser = userDAO.lookup(userid);
			if (button.equals("viewrespondent")){
				 synchronized(session) {
			        	session.setAttribute("user",showuser);
			        }
				return "respondent.jsp";
			}
			if (button.equals("viewresearcher")){
				 synchronized(session) {
			        	session.setAttribute("user",showuser);
			        }
				return "researcher.jsp";
			}
		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if(button.equals("Delete")){

        try {
        	DeleteUserForm form = formBeanFactory.create(request);
        	
        	if (!form.isPresent()) {
        		return "admin.jsp";
        	}
        	
        	request.setAttribute("deleteuserform",form);
    		//Do Validation,if there was something wrong, return to the homepage.
    		errors.addAll(form.checkDeleteUserFormErrors());
    		if (errors.size() != 0) {
    			return "admin.jsp";
    		}

    		// Look up the user gonna be deleted.
    		User user = userDAO.lookup(form.getDeleteuserid());
    		if (user == null) {
    			errors.add("The user does not exist!");
    			return "admin.jsp";
    		} 
	        errors.add("user : " + user.getUserid() + " has been deleted!");
        	// Delete the user
	        
    		userDAO.delete(user.getUserid());
    		
	        synchronized(session) {
	        	session.setAttribute("deleteuser",user);
	        }
			return "admin.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "errors.jsp";
        } catch (FormBeanException e) {
        	errors.add(e.getMessage());
        	return "errors.jsp";
        }
		}
		return "admin.jsp";
    }
}
